class util {
  formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
  }

  gettoplist(){
    return [
      {name:'不置顶',day:0,money:0},
      {name:'置顶1天',day:1,money:1},
      {name:'置顶3天',day:3,money:3},
      {name:'置顶5天',day:5,money:5},
      {name:'置顶7天',day:7,money:7},
      {name:'置顶15天',day:15,money:15},
      {name:'置顶30天',day:30,money:30}
    ];
  }

  nocancelmodal(content,title,confirm,cancel){
    wx.showModal({
      title:title||"提示",
      content,
      showCancel:!1,
      success:res=>{
        if(res.confirm) confirm&&confirm()
        if (res.cancel) cancel && cancel()
      }
    })
  }

  /**
   * 转换时间
   */
  myTime(date) {
    var arr = date.split("T");
    var d = arr[0];
    var darr = d.split('-');
    var t = arr[1];
    var tarr = t.split('.000');
    var marr = tarr[0].split(':');
    var dd = parseInt(darr[0]) + "-" + parseInt(darr[1]) + "-" + parseInt(darr[2]) + " " + parseInt(marr[0]) + ":" + parseInt(marr[1]) + ":" + parseInt(marr[2]);
    return dd;
  }



  formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return [year, month, day].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join('-') + ' ' + [hour, minute, second].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join(':')
  }
  formatTime1 = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return [year, month, day].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join('.') + ' ' + [hour, minute].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join('.')
  }


  formatDate = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    return [year, month, day].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join('-')
  }
  formatDate1 = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    return [month, day].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join('-')
  }
  //手机号加密
  sub = (str, startLength, endLength) => {
    if (str.length == 0 || str == undefined) {
      return "";
    }
    var length = str.length;
    if (length >= startLength + endLength) {
      return str.substring(0, startLength) + "****" + str.substring(length - endLength, length);
    } else {
      return str;
    }
  }



  todate(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    // minute = minute < 10 ? ('0' + minute) : minute;
    // second = second < 10 ? ('0' + second) : second;
    return y + '年' + m + '月' + d + '日';
  }
  toDate(number) {
    var n = number;
    console.log(number, "number");
    var date = new Date(parseInt(n) * 1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    // minute = minute < 10 ? ('0' + minute) : minute;
    // second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d;
  }

  toDate1(number) {
    var n = number;
    var date = new Date(parseInt(n) * 1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    // minute = minute < 10 ? ('0' + minute) : minute;
    // second = second < 10 ? ('0' + second) : second;

    return [y, m, d].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join('-') + ' ' + [h, minute, second].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join(':')
  }
  //月日
  formatMD(date, addDayCount) {
    var dd = date;
    dd.setDate(dd.getDate() + addDayCount)
    var month = dd.getMonth() + 1
    var day = dd.getDate()
    return month + "-" + day;
  }

  //当前日期加减天数
  mathChangeDate(date, method, days) {
    //method:'+' || '-'
    //ios不解析带'-'的日期格式，要转成'/'，不然Nan，切记
    var dateVal = date.replace(/-/g, '/');
    var timestamp = Date.parse(date);
    if (method == '+') {
      timestamp = timestamp / 1000 + 24 * 60 * 60 * days;
    } else if (method == '-') {
      timestamp = timestamp / 1000 - 24 * 60 * 60 * days;
    }
    return timestamp;
  }

  decodeurl(str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&amp;/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    return s;
  }
  //去重
  unique5(arr) {
    var x = new Set(arr);
    return [...x];
  }

  setwatcher(data, watch) {
    Object.keys(watch).forEach(x => {
      this.observer(data, x, watch[x])
    })
  }

  creatpost(that, succ) {
    let app = getApp();
    wx.showLoading({
      title: '图片生成中',
    })
    if (app.posturl) {
      wx.hideLoading({
        success: (res) => { },
      })
      succ && succ(app.posturl)
      return;
    }
    let { shareurl, user } = app.logininfo, { avatarUrl, nickName } = user, shareword = '日日添金';
    let post = `https://haoji.haojijijin.com/czlapi/imgs/${shareurl}`, word = shareword, qrcode = require('../utils/qrcode');
    console.log(post, 'postpost');
    wx.getSystemInfo({
      success: (result) => {
        let { windowWidth, windowHeight, statusBarHeight, screenWidth } = result;
        console.log(statusBarHeight, 'statusBarHeightstatusBarHeight');
        new qrcode({
          width: screenWidth * 0.22,
          height: screenWidth * 0.22,
          canvasId: 'canvas1',
          text: "https://haoji.haojijijin.com/czlapi?id=" + app.logininfo.user.id,
          callback: res => {
            setTimeout(() => {
              wx.canvasToTempFilePath({
                canvasId: 'canvas1',
                quality: '0',
                width: screenWidth * 0.56,
                height: screenWidth * 0.56,
                destWidth: screenWidth * 0.56,
                destHeight: screenWidth * 0.56,
                complete: res => {
                  console.log(res, 'canvasToTempFilePath1s');
                },
                success: res => {
                  let qrpath = res.tempFilePath, coc = wx.createCanvasContext('canvas2', that);
                  wx.getImageInfo({
                    src: post,
                    success: res => {
                      let posttmp = res.path;
                      console.log(avatarUrl, 'avatarUrl');
                      wx.getImageInfo({
                        src: avatarUrl,
                        complete: res => {
                          console.log(res, 'getImageInfo2');
                        },
                        success: res => {
                          console.log(res, 'resresres');
                          let avatartmp = res.path;
                          if (statusBarHeight > 20) {
                            windowHeight = windowHeight * 0.9
                          }
                          coc.drawImage(posttmp, 0, 0, windowWidth, windowWidth * 1.78)
                          coc.rect(windowWidth * 0.1, windowWidth * 1.2, windowWidth * 0.8, windowHeight * 0.25)
                          coc.setFillStyle('white')
                          coc.fill()
                          coc.setFillStyle('#666666')
                          coc.setFontSize(10)
                          coc.fillText(nickName, windowWidth * 0.15, windowWidth * 1.5)
                          coc.setFillStyle('black')
                          coc.setFontSize(12)
                          coc.fillText(word, windowWidth * 0.15, windowWidth * 1.55)
                          coc.drawImage(qrpath, windowWidth * 0.615, windowWidth * 1.3, screenWidth * 0.22, screenWidth * 0.22)
                          // coc.save()
                          coc.arc(windowWidth * 0.15 + 33, windowWidth * 1.2 + 48, 33, 0, 2 * Math.PI)
                          coc.clip();
                          coc.drawImage(avatartmp, windowWidth * 0.15, windowWidth * 1.2 + 15, 66, 66)
                          //coc.drawImage(avatartmp, 0,0, 66, 66)
                          coc.restore();
                          coc.draw(!0, () => {
                            console.log('herererer');
                            wx.canvasToTempFilePath({
                              canvasId: "canvas2",
                              success: res => {
                                wx.hideLoading({
                                  success: (res) => { },
                                })
                                console.log('here');
                                app.posturl = res.tempFilePath;
                                succ && succ(res.tempFilePath)
                                //  that.setData({canvasimg:res.tempFilePath,showpost:!0})
                              }
                            })
                          })
                        }
                      })
                    }
                  })
                }
              })
            }, 100);
          }
        })

      },
    })
  }
  observer(obj, key, watchFun) {
    var val = obj[key]; // 给该属性设默认值
    Object.defineProperty(obj, key, {
      configurable: true,
      enumerable: true,
      set: function (value) {
        val = value;
        watchFun(value, val); // 赋值(set)时，调用对应函数
      },
      get: function () {
        return val;
      }
    })
  }

}

module.exports = util